//import {massAttributeSettingManager} from "./MassAttributeSettingManager";
class SectionAttributeToolManager {
  constructor() {
    this.sectionAttributeInfo = {};//截面属性
  }

  reset() {
    this.sectionAttributeInfo = {};//截面属性
  }

  parseSectionAttributeInfos(responseInfo) {
    this.sectionAttributeInfo = {};
    let lengthUnit = responseInfo.LengthUnit;
    let precision = responseInfo.LengthPrecision;
    for (let attributeName in responseInfo) {
      if (responseInfo.hasOwnProperty(attributeName)) {
        let unit = this.getUnitByName(attributeName, lengthUnit);
        if (attributeName === 'BaryCenter') {
          this.sectionAttributeInfo[attributeName] = {
            name: attributeName,
            unit: unit,
            x: {
              name: 'x',
              value: this.setValue(unit, responseInfo[attributeName].x, attributeName, precision),
            },
            y: {
              name: 'y',
              value: this.setValue(unit, responseInfo[attributeName].y, attributeName, precision),
            },
            z: {
              name: 'z',
              value: this.setValue(unit, responseInfo[attributeName].z, attributeName, precision),
            },
          };
        } else if (attributeName === 'ProuductIneria') {
          let valList = [];
          responseInfo[attributeName].forEach(value => {
            valList.push(this.setValue(unit, value, attributeName, precision));
          });
          let [Lxx, Lxy, Lxz,
            Lyx, Lyy, Lyz,
            Lzx, Lzy, Lzz] = valList;
          this.sectionAttributeInfo[attributeName] = {
            name: attributeName,
            value: {
              Lxx, Lxy, Lxz,
              Lyx, Lyy, Lyz,
              Lzx, Lzy, Lzz,
            },
            unit: unit,
          };
        } else if (attributeName === 'AreaInertia') {
          let valList = [];
          responseInfo[attributeName].forEach(value => {
            valList.push(this.setValue(null, value, attributeName, precision));
          });
          let [Iy, Iz] = valList;
          this.sectionAttributeInfo[attributeName] = {
            name: attributeName,
            value: {
              Iy,
              Iz,
            },
            unit: unit,
          };
        } else if (attributeName === 'PolarIneria' || attributeName === 'ProductIneria') {
          let valList = responseInfo[attributeName];
          this.sectionAttributeInfo[attributeName] = {
            name: attributeName,
            value: this.setValue(unit, valList, attributeName, precision),
            unit: unit,
          };
        } else if (attributeName === 'Area') {
          let valList = responseInfo[attributeName];
          this.sectionAttributeInfo[attributeName] = {
            name: attributeName,
            value: this.setValue(unit, valList, attributeName, precision),
            unit: unit,
          };
        } else if (attributeName === 'RadiusInertia') {
          let valList = [];
          responseInfo[attributeName].forEach(value => {
            valList.push(this.setValue(null, value, attributeName, precision));
          });
          let [iy, iz] = valList;
          this.sectionAttributeInfo[attributeName] = {
            name: attributeName,
            value: {
              iy,
              iz,
            },
            unit: unit,
          };
        }
      }
    }
  }

  //获取对应的单位
   getUnitByName(name, lengthUnit) {
    if (name === 'BaryCenter') {
      return lengthUnit;
    }
    if (name === 'Area') {
      return lengthUnit + '²';
    }
    if (['AreaInertia', 'PolarIneria', 'ProuductIneria', 'ProductIneria'].includes(name)) {
      return lengthUnit + '⁴';
    }
    if (name === 'RadiusInertia') {
      return lengthUnit;
    }
  }

  //设置精度后的数值
  setValue(unit, value, name, precision) {
    let n = precision; //!<保留三位小数
    let t = 1;
    for (; n > 0; t *= 10, n--) ;
    for (; n > 0; t /= 10, n++) ;
    value = Math.round(value * t) / t;

    if (typeof value === 'number') {
      value = value.toFixed(precision);
    }

    return value;
  }
}
const sectionAttributeToolManager = new SectionAttributeToolManager();
export { sectionAttributeToolManager };
